<?php
/**
 * Created by PhpStorm.
 * User: fengzi
 * Date: 2019/6/3
 * Time: 14:34
 */

namespace app\vehicle\logic;
use app\vehicle\model\Volunteer;
use app\vehicle\model\Activity;
use app\vehicle\model\ActivityLog;
use app\vehicle\model\Practice;
use app\vehicle\model\PracticeLog;
use app\vehicle\model\UserTpl;
use app\common\model\Site;
use app\common\model\SiteTeam;
use app\common\model\Organization;
use app\vehicle\logic\TplLogic;
class activityLogic
{
    public function setActivitySign($data,$user_id){
        $info = Practice::where('id','eq',$data['id'])->field('*,end_time as e_time')->find();
        if(!$info['id']){
            return '活动不存在';
        }
        $d = PracticeLog::where('user_id','eq',$user_id)->where('aid','eq',$data['id'])->find();
        if(!$d['id']){
            return '您未报名该活动';
        }
        $time = time();
        if($data['type_id']==1){
            $s_time = PracticeLog::where('user_id','eq',$user_id)->where('aid','eq',$data['id'])->value('s_time');
            if(!$s_time) {
                if($info['activity_use_time']-1800>$time){
                    return '活动未开始';
                }
                PracticeLog::where('id', 'eq', $d['id'])->update(['status' => 1, 's_time' => time()]);
                return '签名成功';
            }else{
                return '已签到';
            }
        }else if($data['type_id']==2){
            $e_time = PracticeLog::where('user_id','eq',$user_id)->where('aid','eq',$data['id'])->value('e_time');
            if(!$e_time) {
                if ($info['activity_end_time'] + 1800 < $time) {
                    return '活动已开始';
                }
                $t = $info['e_time'] > time() ? $info['e_time'] : time();
                PracticeLog::where('id', 'eq', $d['id'])->update(['status' => 2, 'e_time' => $t]);
                return '退签成功';
            }else{
                return '已退签';
            }
        }
        return 1;
    }
//    public function setActivitySign($data,$user_id){
//        $info = Practice::where('id','eq',$data['id'])->field('*,end_time as e_time')->find();
//        if(!$info['id']){
//            return '活动不存在';
//        }
//        $d = PracticeLog::where('user_id','eq',$user_id)->where('aid','eq',$data['id'])->find();
//        if(!$d['id']){
//            return '您未报名该活动';
//        }else if($d['e_time']){
//            return '已退签';
//        }
//        $time = time();
//        $s_time = PracticeLog::where('user_id','eq',$user_id)->where('aid','eq',$data['id'])->value('s_time');
//        if(!$s_time) {
//            if($info['activity_use_time']-1800>$time){
//                return '活动未开始';
//            }
//            PracticeLog::where('id', 'eq', $d['id'])->update(['status' => 1, 's_time' => time()]);
//            return '签名成功';
//        }else{
//            if($info['activity_end_time']+1800<$time){
//                return '活动已开始';
//            }
//            $t = $info['e_time']>time()?$info['e_time']:time();
//            PracticeLog::where('id', 'eq', $d['id'])->update(['status' => 2,'e_time' => $t]);
//            return '退签成功';
//        }
//        return 1;
//    }
    public function getUserActivityList($data,$user_id){
        $where[] = ['status','eq',$data['status']];
        $where[] = ['user_id','eq',$user_id];
        $list = Practice::where($where)->select();

        return $list;
    }
    public function getNoPracticeList($data,$user_id){
        $where[] = ['status','eq',1];
        $where[] = ['type_id','=',1];
        $list = Practice::where($where)->order('create_time desc')->page($data['page'],100)->select();
        foreach($list as $k=>$v){
            $list[$k]['is_apply'] = PracticeLog::where('user_id','eq',$user_id)->where('aid','eq',$v['id'])->count();
        }
        return $list;
    }
    public function getPracticeList($data,$user_id){
        $where[] = ['status','eq',1];
        $where[] = ['is_end','eq',0];
        $where[] = ['use_time','<',time()];
        $where[] = ['end_time','>',time()];
        $where[] = ['type_id','=',0];
        $list = Practice::where($where)->field('*')->order('create_time desc')->page($data['page'],100)->select();
        foreach($list as $k=>$v){
            $list[$k]['is_apply'] = PracticeLog::where('user_id','eq',$user_id)->where('aid','eq',$v['id'])->count();
            $list[$k]['end_time'] = $v['activity_use_time'];
        }
        return $list;
    }
    public function getPracticeInfo($data,$user_id){
        Practice::where('id','eq',$data['id'])->setInc('read_num',1);
        $info = Practice::get($data['id']);
        $log = PracticeLog::where('user_id','eq',$user_id)->where('aid','eq',$info['id'])->find();
        $info['is_apply'] = $log['id']??0;
        $info['s_time'] = $log['s_time'];
        $info['e_time'] = $log['e_time'];
//        if($user_id==$info['user_id']){
//            $info['is_user'] = 1;
//        }
        $info['activity_use_time'] = date('Y-m-d H:i',$info['activity_use_time']);
        $info['activity_end_time'] = date('Y-m-d H:i',$info['activity_end_time']);
        $info['content'] = str_replace('/ueditor/php/upload/image',config('database.img_url').'/ueditor/php/upload/image',$info['content']);
        $info['content'] = htmlspecialchars_decode($info['content']);
        return $info;
    }
    public function setPractice($data,$user_id,$openid){
        $check = PracticeLog::where('user_id','eq',$user_id)->where('aid','eq',$data['id'])->count();
        if($check){
            return '已报名';
        }
        $info = Practice::where('id','eq',$data['id'])->field('*,end_time as e_times')->find();
        if($info['e_times']<time()){
            return '活动已结束';
        }else if($info['status']!=1){
            return '该活动未审核';
        }
        if($data['volunteer']){
            $num = PracticeLog::where('aid','eq',$data['id'])->where('volunteer','eq',1)->count();
            if($num>=$info['number']){
                return '志愿者人数已满';
            }
        }else{
            $num = PracticeLog::where('aid','eq',$data['id'])->where('volunteer','eq',0)->count();
            if($num>=$info['numbers']){
                return '群众人数已满';
            }
        }
        $volunteer = Volunteer::where('user_id','eq',$user_id)->find();
        $insert['user_id'] = $user_id;
        if($volunteer) {
            $insert['oid'] = $volunteer['oid'];
            $insert['site_id'] = $volunteer['site_id'];
        }else{
            $insert['oid'] = 0;
            $insert['site_id']=0;
        }
        $insert['aid'] = $data['id'];
        $insert['realname'] = $data['realname'];
        $insert['sex'] = $data['sex'];
        $insert['mobile'] = $data['mobile'];
        $insert['id_card'] = $data['id_card'];
        $insert['volunteer'] = $data['volunteer'];
        $insert['create_time'] = time();
        PracticeLog::insert($insert);

        Practice::where('id','eq',$data['id'])->setInc('apply_number');
//        if($volunteer['site_id']){
//            Site::where('id','eq',$volunteer['site_id'])->setInc('number');
//        }
//        if($volunteer['oid']){
//            SiteTeam::where('id','eq',$volunteer['oid'])->setInc('number');
//        }
        TplLogic::applyTpl($openid,$info['title'],'报名成功',date('Y-m-d H:i',$info['activity_use_time']),$info['address']);
        return 1;
    }
    public function getOrderList($data,$user_id){
        $where[] = ['status','eq',$data['status']];
        $where[] = ['user_id','eq',$user_id];

        $list = PracticeLog::with('practice')->where($where)->select();
        foreach($list as $k=>$v){
            $list[$k]['apply_num'] = PracticeLog::where('aid','eq',$v['aid'])->count();
        }
        return $list;
    }
    public function apply_activity(){

    }
    public function setActivity($data,$user_id){
        $check = ActivityLog::where('user_id','eq',$user_id)->where('aid','eq',$data['id'])->count();
        if($check){
            return '已报名';
        }
        $num = ActivityLog::where('aid','eq',$data['id'])->count();
        $info = Activity::get($data['id']);
        if($num>=$info['number']){
            return '活动人数已满';
        }
        Activity::where('id','eq',$data['id'])->setInc('apply_number');
        $insert['type_id'] = $data['type_id'];
        $insert['user_id'] = $user_id;
        $insert['aid'] = $data['id'];
        $insert['create_time'] = time();
        ActivityLog::insert($insert);
        return 1;
    }
    public function getActivityList($data,$user_id){
        
        $where[] = ['status','eq',1];
        $where[] = ['use_time','<',time()];
        $where[] = ['end_time','>',time()];
        $list = Activity::where($where)->order('create_time desc')->page($data['page'],10)->select();
        foreach($list as $k=>$v){
            $list[$k]['is_apply'] = ActivityLog::where('user_id','eq',$user_id)->where('aid','eq',$v['id'])->count();
        }
        return $list;
    }
    public function getList(){
        $where[] = ['status','eq',1];
        $where[] = ['is_end','eq',0];
        //$where[] = ['use_time','>',time()];
        $where[] = ['end_time','>',time()];
        $where[] = ['type_id','=',0];
        $list = Practice::where($where)->order('create_time desc')->limit(3)->select();
        return $list;
    }
    public function getArticleList($data){
        $where[] = ['status','eq',1];
        $where[] = ['use_time','>',time()];
        $list = Activity::where($where)->order('create_time desc')->page($data['page'],100)->select();
        return $list;
    }
    public function getArticleInfo($data,$user_id){
        Activity::where('id','eq',$data['id'])->setInc('read_num',1);
        $info = Activity::get($data['id']);
        $info['is_apply'] = ActivityLog::where('user_id','eq',$user_id)->where('aid','eq',$info['id'])->count();
        $info['content'] = str_replace('/ueditor/php/upload/image',config('database.img_url').'/ueditor/php/upload/image',$info['content']);
        $info['content'] = htmlspecialchars_decode($info['content']);
        return $info;
    }
    public function addActivity($data,$user){
        if($user['team_admin']) {
            $data['oid'] = $user['team_admin'];
        }else{
            $volunteer = Volunteer::where('user_id','eq',$user['id'])->find();
            if($volunteer['oid']){
                $data['oid'] = $volunteer['oid'];
            }else{
                $data['oid'] = 0;
            }
        }
        $data['user_id'] = $user['id'];
        $data['use_time'] = strtotime($data['use_time']);
        $data['end_time'] = strtotime($data['end_time']);
        $data['activity_use_time'] = strtotime($data['activity_use_time']);
        $data['activity_end_time'] = strtotime($data['activity_end_time']);

        $data['create_time'] = time();
        $res = Practice::insert($data);
        if($res){
            return 1;
        }else{
            return '发布失败';
        }
    }

    public function canclePractice($data,$user_id){
        $check = PracticeLog::where('id','eq',$data['id'])->where('user_id','eq',$user_id)->count();
        if(!$check){
            return '未报名';
        }
        return PracticeLog::where('aid','eq',$data['id'])->where('user_id','eq',$user_id)->delete();
    }

    public function setReport($data,$user_id){
        $info = Practice::where('id','eq',$data['id'])->find();
        if($info['report_contents']){
            return '已汇报';
        }
        $up['report_contents'] = $data['report_contents'];
        $up['img1'] = $data['up_img'][0];
        $up['img2'] = $data['up_img'][1];
        $up['img3'] = $data['up_img'][2];
        $up['update_time'] = time();
        $res = Practice::where('id','eq',$data['id'])->update($up);
        return 1;
    }
}